package com.atguigu.gmall.search.rpc;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.search.Goods;
import com.atguigu.gmall.search.service.SearchService;
import com.atguigu.gmall.search.vo.SearchParamVo;
import com.atguigu.gmall.search.vo.SearchResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author:lyd
 * @Date:2023/8/26 9:28
 * @Version:1.0
 * @Description
 **/
@RestController
@RequestMapping("/api/inner/search")
public class SearchController {

    @Autowired
    private SearchService searchService;


    /**
     * 更新es中的热度分
     * @param skuId 商品id
     * @param score 热度分
     * @return
     */
    @GetMapping("/inchotscore/{skuId}/{score}")
    public Result incrHotScore(@PathVariable Long skuId, @PathVariable Long score) {
        searchService.incrHotScore(skuId, score);
        return Result.ok();
    }

    /**
     *根据传递的请求参数SearchParamVo，查询页面需要的数据
     * @param param
     * @return
     */
    @PostMapping("/search")
    public Result<SearchResultVo> search(@RequestBody SearchParamVo param) {
        SearchResultVo resultVo = searchService.search(param);
        return Result.ok(resultVo);
    }

    /**
     * 保存商品在es中
     *
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody Goods goods) {
        searchService.save(goods);
        return Result.ok();
    }

    /**
     * 根据id，从es中删除商品
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteGoods(@PathVariable Long id) {
        searchService.deleteGoods(id);
        return Result.ok();
    }
}
